package com.example.ISearchBlog.controller;

import com.example.ISearchBlog.common.*;
import com.example.ISearchBlog.entity.UserInfo;
import com.example.ISearchBlog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CheckCodeTools codeTools;

    @RequestMapping("/reg")
    public AjaxResult reg(UserInfo userInfo) {
        //前端已经进行非空效验，后端永远不要相信前端
        //1.进行非空判断
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
        !StringUtils.hasLength(userInfo.getPassword()))
            //StringUtils.hasLength既可以判断null也可以判断空
            return AjaxResult.fail(-1, "参数有误");
        //2.调用 UserService 执行添加方法，并将返回的结果添加 AjaxResult.data进行返回
        //将密码进行加盐加密
        userInfo.setPassword(PasswordTools.encrypt(userInfo.getPassword()));
        int result = userService.reg(userInfo);
        return AjaxResult.success(result);
    }

    @RequestMapping("/login")
    public AjaxResult login(String username, String password, String code_input,
                            HttpServletRequest request) {
        System.out.println("当前用户输入的验证码：" + code_input);
        if (username == null || !StringUtils.hasLength(username) ||
                !StringUtils.hasLength(password))
            //StringUtils.hasLength既可以判断null也可以判断空
            return AjaxResult.fail(-1, "参数有误");
        UserInfo userInfo = userService.login(username);
        if (userInfo == null || userInfo.getId() <= 0)
            return AjaxResult.fail(-2, "用户名或密码错误！");
        if (!PasswordTools.decrypt(password, userInfo.getPassword()))
            return AjaxResult.fail(-2, "用户名或密码错误！");
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute(ApplicationVariable.SESSION_KEY_CHECK_CODE);
        System.out.println("存储的验证码：" + code);
        if (!code_input.equals(code)) {
            return AjaxResult.fail(-3, "用户验证码错误");
        }
        //将当前成功登录的用户信息存储到 session
        session.setAttribute(ApplicationVariable.SESSION_KEY_USERINFO, userInfo);

        return AjaxResult.success(1);
    }

    //注销操作
    @RequestMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(ApplicationVariable.SESSION_KEY_USERINFO);//移除当前用户的 session
        return AjaxResult.success(1);
    }

    //判断当前用户是否已经登录
    @RequestMapping("/islogin")
    public AjaxResult isLogin(HttpServletRequest request) {
        if (UserSessionTools.getLoginUser(request) == null) {
            //未登录
            return AjaxResult.success(0);
        }
        return AjaxResult.success(1);
    }

    //登录时获取验证码
    @RequestMapping("/getcode")
    public AjaxResult getCode(HttpServletRequest request) {
        String[] codeArr = codeTools.createImage();
        //将验证码存储到 session
        HttpSession session = request.getSession();
        session.setAttribute(ApplicationVariable.SESSION_KEY_CHECK_CODE, codeArr[1]);
        return AjaxResult.success("/image/" + codeArr[0]);
    }
}
